package com.example.yogurtsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yogurtsystem.common.PageResult;
import com.example.yogurtsystem.dto.ResultDTO;
import com.example.yogurtsystem.entity.Drinks;
import com.example.yogurtsystem.mapper.DrinksMapper;
import com.example.yogurtsystem.service.DrinksService;
import com.example.yogurtsystem.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ZXB
 * @version 1.0.0
 * @ClassName DrinksServiceImpl
 * @Description TODO
 * @Date 2021/8/14 15:45
 */
@Service
public class DrinksServiceImpl implements DrinksService {

    @Autowired
    private DrinksMapper drinksMapper;

    @Override
    public void insertDrinks(Drinks drinks) {
        drinksMapper.insert(drinks);
    }

    @Override
    public void updateDrinks(Drinks drinks) {
        drinks.setUpdateTime(new Date());
        drinksMapper.updateById(drinks);
    }

    @Override
    public void deleteDrinks(String id) {
        drinksMapper.deleteById(id);
    }

    @Override
    public ResultDTO selectDrinksByLimit(int pageNum, int pageSize, String name) {
        QueryWrapper<Drinks> queryWrapper=new QueryWrapper<>();
        //如果名称不为空则模糊查找名称
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        Page<Drinks> page=new Page<>(pageNum,pageSize);
        IPage<Drinks> iPage = drinksMapper.selectPage(page, queryWrapper);
        PageResult pageResult = PageUtil.pageResultCopy(iPage);
        return ResultDTO.requestSuccess(pageResult);
    }

    @Override
    public List<Drinks> getDrinkList() {
        return drinksMapper.selectList(null);
    }
}
